package com.filebackup.struts.actions;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.filebackup.business.LocalFileService;
import com.filebackup.business.LocalFolderService;
import com.filebackup.business.ShareService;
import com.filebackup.vo.Vo;
import com.filebackup.vo.impl.LocalFileVo;
import com.filebackup.vo.impl.LocalFolderVo;
import com.filebackup.vo.impl.SendShareVo;
import com.filebackup.vo.impl.SendSyncVo;
import com.filebackup.vo.impl.TableVo;

public class SendShareAction extends FilebackupSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ShareService shareService;
	private LocalFolderService localFolderService;
	private LocalFileService localFileService;
	
	public String execute() throws Exception{
		JSONArray fileArr=new JSONArray();
		JSONArray folderArr=new JSONArray();
		List<TableVo> tableList=this.getShareService().getOtherShare(this.getSessionUser().getId());
		for(int i=0;i<tableList.size();i++){
			List<Vo> voList=this.getLocalFileService().findAllSubFile(tableList.get(i).getId());
			for(int j=0;j<voList.size();j++){
				SendShareVo vo=new SendShareVo();
				vo.setPath(this.getLocalFileService().returnPath(((LocalFileVo)voList.get(i)).getId()));
				vo.setId(((LocalFileVo)voList.get(i)).getId());
				vo.setShareOwnerId(((LocalFileVo)voList.get(i)).getOwnerId());
				vo.setVersion(((LocalFileVo)voList.get(i)).getVersion());
				fileArr.add(vo);
			}
			List<Vo> folderList=this.getLocalFolderService().findAllSubFolder(tableList.get(i).getId());
			for (int j = 0; j < folderList.size();j++) {
				SendShareVo vo = new SendShareVo();
				vo.setId(((LocalFolderVo) folderList.get(i)).getId());
				vo.setPath(this.getLocalFolderService().returnPath(
						((LocalFolderVo) folderList.get(i)).getId()));
				vo.setVersion(((LocalFolderVo) folderList.get(i)).getVersion());
				vo.setShareOwnerId(((LocalFolderVo) folderList.get(i)).getOwnerId());
				folderArr.add(vo);
			}
			
		}
		JSONObject obj=new JSONObject();
		obj.put("folder", folderArr);
		obj.put("file", fileArr);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(obj.toString());
			response.flushBuffer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				response.getWriter().close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
		
	}
	public void setShareService(ShareService shareService) {
		this.shareService = shareService;
	}
	public ShareService getShareService() {
		return shareService;
	}
	public void setLocalFolderService(LocalFolderService localFolderService) {
		this.localFolderService = localFolderService;
	}
	public LocalFolderService getLocalFolderService() {
		return localFolderService;
	}
	public void setLocalFileService(LocalFileService localFileService) {
		this.localFileService = localFileService;
	}
	public LocalFileService getLocalFileService() {
		return localFileService;
	}

}
